package com.whut.service.impl;

import com.whut.mapper.NewprefixsystemMapper;
import com.whut.mapper.SampleinfoMapper;
import com.whut.model.Newprefixsystem;
import com.whut.model.Sampleinfo;
import com.whut.service.SampleEquipService;
import com.whut.v_model.MachineSampled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

@Service("SampleEquipService")
public class SampleEquipImpl implements SampleEquipService {
    @Resource
    public SampleinfoMapper SampleinfoMapper;

    @Resource
    private NewprefixsystemMapper newprefixsystemMapper;

    @Override
    public ArrayList getSampleByProjectId(Integer projectId,Integer taskId) {
        ArrayList arr = SampleinfoMapper.getSampleByProjectId(projectId,taskId);
        if (arr.size() == 0) {
            Sampleinfo sampleinfo = new Sampleinfo();
            sampleinfo.setSampleName(projectId.toString());
            sampleinfo.setTaskId(taskId);
            sampleinfo.setProjectId(projectId);
            sampleinfo.setSampleType(0);
            sampleinfo.setSampleUnit("");
            sampleinfo.setSampleNum("");
            sampleinfo.setSampleIndex("");
            sampleinfo.setOriginSampleIndex("");
            sampleinfo.setRangeNum1(2);
            sampleinfo.setRangeNum2(3);
            sampleinfo.setRangeNum3(4);
            sampleinfo.setModifyReason("暂无修改");
            SampleinfoMapper.insertSelective(sampleinfo);
            arr = SampleinfoMapper.getSampleByProjectId(projectId,taskId);
            return arr;
        } else {
            return arr;
        }
    }

    @Override
    public int changeSampleType(Integer mKey, Integer sampleType) {
        SampleinfoMapper.uptateSampleType(mKey, sampleType);
        return 0;
    }

    @Override
    public int changeNum(Integer mKey, Integer rangeNum1, Integer rangeNum2, Integer rangeNum3) {
        SampleinfoMapper.uptateNum(mKey, rangeNum1, rangeNum2, rangeNum3);
        return 0;
    }

    @Override
    public int saveSampleDetail(Integer mKey, String sampleNum, String originSampleIndex, String sampleIndex, String sampleUnit, String modifyReason) {
        Sampleinfo sampleinfo = new Sampleinfo();
        sampleinfo.setmKey(mKey);
        sampleinfo.setSampleNum(sampleNum);
        sampleinfo.setOriginSampleIndex(originSampleIndex);
        sampleinfo.setSampleIndex(sampleIndex);
        sampleinfo.setSampleUnit(sampleUnit);
        sampleinfo.setModifyReason(modifyReason);
        SampleinfoMapper.updateByPrimaryKeySelective(sampleinfo);
        return 0;
    }

    @Override
    public ArrayList<MachineSampled> getAllEquipment(Integer projectId) {
        ArrayList<MachineSampled> arr = SampleinfoMapper.getAllEquipment(projectId);
        arr.forEach(item -> {
            Newprefixsystem newprefixsystem = newprefixsystemMapper.getFather(item.getEquipType());
            if(newprefixsystem!=null){
                item.setEquipTypeName(newprefixsystem.getParameterName());
            }

        });
        return arr;
    }

}
